#include "paint.h"
#include "landdialog.h"
#include <QMouseEvent>
paint::paint(QChartView *parent) : QChartView(parent)
{
    fPointX_month = 0;
    fPointX_week = 0;
    //设置一些QPen，以便待会设置线条的颜色，宽度
    QPen p0 ,p1,p2;
    //p0温度
    p0.setWidth(1);
    p0.setColor(Qt::blue);

    //p1湿度
    p1.setWidth(1);
    p1.setColor(Qt::red);

    p2.setWidth(1);
    p2.setColor(Qt::black);
    //设置线
    series0 = new QLineSeries(this);  //温度
    series0->setPen(p0);              //画笔格式
    series0->setName("温度");          //图例
    series1 = new QLineSeries(this);  //湿度
    series1->setPen(p1);
    series1->setName("湿度");
    series2 = new QLineSeries(this);  //光照度
    series2->setPen(p2);
    series2->setName("光照度");

    //设置线-月
    series0_month = new QSplineSeries(this);  //温度
    series0_month->setPen(p0);              //画笔格式
    series0_month->setName("温度");          //图例
    series1_month = new QSplineSeries(this);  //湿度
    series1_month->setPen(p1);
    series1_month->setName("湿度");
    series2_month = new QSplineSeries(this);  //光照度
    series2_month->setPen(p2);
    series2_month->setName("光照度");

    //设置线-周
    series0_week = new QSplineSeries(this);  //温度
    series0_week->setPen(p0);              //画笔格式
    series0_week->setName("温度");          //图例
    series1_week = new QSplineSeries(this);  //湿度
    series1_week->setPen(p1);
    series1_week->setName("湿度");
    series2_week = new QSplineSeries(this);  //光照度
    series2_week->setPen(p2);
    series2_week->setName("光照度");
    /********************************/
    /*申请坐标轴*/
    axisY_day = new QValueAxis(this);
    axisY_lux_day = new QValueAxis(this);
    axisX_day = new QValueAxis(this);

    axisY_week = new QValueAxis(this);
    axisY_lux_week = new QValueAxis(this);
    axisX_week = new QValueAxis(this);

    axisY_month = new QValueAxis(this);
    axisY_lux_month = new QValueAxis(this);
    axisX_month = new QValueAxis(this);


    //初始fPointX从当前时间节点开始画图
    QTime time;
    int timeMes = (time.currentTime().minute()*60+time.currentTime().msec());
    fPointX = (float)((float)timeMes/(float)(3600));
    connect(&timer,SIGNAL(timeout()),this,SLOT(get_asix_slot()));
    connect(this,SIGNAL(to_paint_signal()),this,SLOT(paint_picture_slot()));
}

paint::~paint()
{
//    delete axisX;
//    delete axisY;
}

void paint::paint_x_axes()   //绘制x坐标
{
    axisX_day->setRange(0,1);    //设置范围
    axisX_day->setLabelFormat("%.1f");   //设置刻度的格式
    axisX_day->setGridLineVisible(true);   //网格线可见
    axisX_day->setTickCount(1);       //设置多少格
    axisX_day->setMinorTickCount(1);   //设置每格小刻度线的数目
    chart->addAxis(axisX_day, Qt::AlignBottom);       //为chart设置X轴,并把轴附加到series线上
    axisX_day->setTitleText("time:h");
    series0->attachAxis(axisX_day);
    series1->attachAxis(axisX_day);
    series2->attachAxis(axisX_day); //光照
}
void paint::paint_y_axes() //绘制y坐标
{
    axisY_day->setRange(0,70);             //设置范围
    axisY_day->setLabelFormat("%d");        //设置刻度的格式
    axisY_day->setGridLineVisible(true);    //网格线可见
    axisY_day->setTickCount(10);             //设置多少格
    axisY_day->setMinorTickCount(5);        //设置每格小刻度线的数目
    chart->addAxis(axisY_day, Qt::AlignLeft);       //为chart设置X轴,并把轴附加到series线上
    axisY_day->setTitleText("C:RH:Eav");
    series0->attachAxis(axisY_day);
    series1->attachAxis(axisY_day);
}
void paint::paint_y_lux_day_axes() //绘制y——lux坐标
{
    axisY_lux_day->setRange(200,400);             //设置范围
    axisY_lux_day->setLabelFormat("%d");        //设置刻度的格式
    axisY_lux_day->setGridLineVisible(true);    //网格线可见
    axisY_lux_day->setTickCount(10);             //设置多少格
    axisY_lux_day->setMinorTickCount(5);        //设置每格小刻度线的数目
    chart->addAxis(axisY_lux_day, Qt::AlignRight);       //为chart设置X轴,并把轴附加到series线上
    series2->attachAxis(axisY_lux_day);
}
void paint::paint_y_lux_week_axes()
{
    axisY_lux_week->setRange(200,400);             //设置范围
    axisY_lux_week->setLabelFormat("%d");        //设置刻度的格式
    axisY_lux_week->setGridLineVisible(true);    //网格线可见
    axisY_lux_week->setTickCount(10);             //设置多少格
    axisY_lux_week->setMinorTickCount(5);        //设置每格小刻度线的数目
    chart_week->addAxis(axisY_lux_week, Qt::AlignRight);
    series2_week->attachAxis(axisY_lux_week);
}
void paint::paint_y_lux_month_axes()
{

    axisY_lux_month->setRange(200,400);             //设置范围
    axisY_lux_month->setLabelFormat("%d");        //设置刻度的格式
    axisY_lux_month->setGridLineVisible(true);    //网格线可见
    axisY_lux_month->setTickCount(10);             //设置多少格
    axisY_lux_month->setMinorTickCount(5);        //设置每格小刻度线的数目
    chart_month->addAxis(axisY_lux_month, Qt::AlignRight);
    series2_month->attachAxis(axisY_lux_month);
}
void paint::paint_month_x_axes()//绘制当月x坐标
{
    axisX_month->setRange(0, 30);    //设置范围
    axisX_month->setLabelFormat("%d");   //设置刻度的格式
    axisX_month->setGridLineVisible(true);   //网格线可见
    axisX_month->setTickCount(10);       //设置多少格
    axisX_month->setMinorTickCount(2);   //设置每格小刻度线的数目
    chart_month->addAxis(axisX_month, Qt::AlignBottom);       //为chart设置X轴,并把轴附加到series线上
    series0_month->attachAxis(axisX_month);
    series1_month->attachAxis(axisX_month);
    series2_month->attachAxis(axisX_month);
}
void paint::paint_month_y_axes()//绘制当月y坐标
{
    axisY_month->setRange(0,70);             //设置范围
    axisY_month->setLabelFormat("%d");        //设置刻度的格式
    axisY_month->setGridLineVisible(true);    //网格线可见
    axisY_month->setTickCount(10);             //设置多少格
    axisY_month->setMinorTickCount(5);        //设置每格小刻度线的数目
    chart_month->addAxis(axisY_month, Qt::AlignLeft);       //为chart设置X轴,并把轴附加到series线上
    series0_month->attachAxis(axisY_month);
    series1_month->attachAxis(axisY_month);
}

void paint::paint_week_x_axes()//绘制当周x坐标
{
    axisX_week->setRange(0,7);    //设置范围
    axisX_week->setLabelFormat("%d");   //设置刻度的格式
    axisX_week->setGridLineVisible(true);   //网格线可见
    axisX_week->setTickCount(6);       //设置多少格
    axisX_week->setMinorTickCount(2);   //设置每格小刻度线的数目
    chart_week->addAxis(axisX_week, Qt::AlignBottom);       //为chart设置X轴,并把轴附加到series线上
    series0_week->attachAxis(axisX_week);
    series1_week->attachAxis(axisX_week);
    series2_week->attachAxis(axisX_week);
}
void paint::paint_week_y_axes()//绘制当周y坐标
{
    axisY_week->setRange(0,70);             //设置范围
    axisY_week->setLabelFormat("%d");        //设置刻度的格式
    axisY_week->setGridLineVisible(true);    //网格线可见
    axisY_week->setTickCount(10);             //设置多少格
    axisY_week->setMinorTickCount(5);        //设置每格小刻度线的数目
    chart_week->addAxis(axisY_week, Qt::AlignLeft);       //为chart设置X轴,并把轴附加到series线上
    series0_week->attachAxis(axisY_week);
    series1_week->attachAxis(axisY_week);
}


void paint::get_x_data()//获取x轴值
{
    float xAxis = this->fPointX;
    xAxis += (float)(10.0/(3600));
    this->fPointX = xAxis;
}
void paint::get_y_data(env_data1 data)//获取y轴值-不要
{
    para.hum = data.hum;
    para.lux = data.lux;
    para.tem = data.tem;
}
void paint::get_asix_slot()
{
    get_x_data();
    get_y_data(from_server.env_data);

    series0->append(fPointX,para.tem);
    series1->append(fPointX,para.hum);
    series2->append(fPointX,para.lux);
    emit to_paint_signal();
}

void paint::paint_picture_slot()
{
    if (fPointX > 1)
    {
        get_y_data(para);
        //直接去再画一编就是第二次0点的
        fPointX = 0;
        series0->clear();
        series1->clear();
        series2->clear();
        emit to_paint_signal();
    }
    emit set_chartView_signal();
}
void paint::get_y_data_week(QVector<env_data1> data)      //Y轴值-week-每天一个平均值总计7个
{
    //把数据加到线上
    qDebug() << "get_y_data_week";
    int i = 0;
    for(i = 0; i < data.count() - 1;i++)
    {
       series0_week->append(fPointX_week,data.at(i).tem);
       series1_week->append(fPointX_week,data.at(i).hum);
       series2_week->append(fPointX_week,data.at(i).lux);
       fPointX_week += (1.0/(24));//1小时画一个点，X轴的分布
    }
}
void paint::get_y_data_month(QVector<env_data1> data)     //Y轴值-month-每天一个天平均值总计30个。
{
    //把数据加到线上
    qDebug() << "get_y_data_month";
    int i = 0;
    for(i = 0; i < data.size() - 1;i++)
    {
       qDebug() << QString().number(data.size());
       qDebug() << QString().number(data.count());
       series0_month->append(fPointX_month,data.at(i).tem);
       series1_month->append(fPointX_month,data.at(i).hum);
       series2_month->append(fPointX_month,data.at(i).lux);
       fPointX_month += (1.0/24); //1小时画一个点，X轴的分布
    }
}
void paint::paint_month_pic()
{
   qDebug() << "paint_month_pic()";
   emit set_chartView_2_signal();
}

void paint::paint_week_pic() //绘制整个周的图
{
    qDebug() << "paint_week_pic()";
    emit set_chartView_3_signal();
}


